package com.abl.common.kit;

import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;

public class FileKit {

    public static void main(String[] args) throws Exception {
        // 指定目录
        File directory = new File("C:\\Users\\Administrator\\Desktop\\project\\bai_jun_ke_ji\\_work\\lilishop");
        // 遍历目录
        ArrayList<File> files = directoryAllFiles(directory, new ArrayList<>());
        System.out.println(files.size());

        ArrayList<String> removeContentList = new ArrayList<>();
        removeContentList.add("import io.swagger.annotations.Api;");
        removeContentList.add("import io.swagger.annotations.ApiImplicitParam;");
        removeContentList.add("import io.swagger.annotations.ApiOperation;");
        removeContentList.add("import org.springframework.beans.factory.annotation.Autowired;");

        for (File file : files) {
            deleteLinesByContent(file, removeContentList);
        }

    }

    public static void replaceLinesByContent(File file, FileLineReplace fileLineReplace) throws Exception {
        ArrayList<FileLineReplace> fileLineReplaceList = new ArrayList<>();
        fileLineReplaceList.add(fileLineReplace);
        replaceLinesByContent(file, fileLineReplaceList);
    }

    public static void replaceLinesByContent(File file, ArrayList<FileLineReplace> fileLineReplaceList) throws Exception {
        // 读取文件内容
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String lineContent;
        int lineNum = 0;

        StringBuilder builder = new StringBuilder();

        //遍历每一行
        while ((lineContent = reader.readLine()) != null) {
            lineNum++;
            String newContent = lineContent;
            for (FileLineReplace fileLineReplace : fileLineReplaceList) {
                newContent = fileLineReplace.replaceLine(lineContent);
            }
            StringKit.formatPrint("替换内容 [] {}行 \n原文：{} \n替换后：{}", file.getName(), lineNum, lineContent,newContent);

            builder.append(newContent);
            builder.append(System.lineSeparator());
        }
        reader.close();

        //将新的文件内容写入原文件中
        FileWriter writer = new FileWriter(file);
        writer.write(builder.toString());
        writer.close();
    }


    public static void deleteLinesByContent(File file, String content) throws Exception {
        ArrayList<String> contentList = new ArrayList<>();
        contentList.add(content);
        deleteLinesByContent(file, contentList);
    }

    public static void deleteLinesByContent(File file, ArrayList<String> contentList) throws Exception {
        // 读取文件内容
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String lineContent;
        int lineNum = 0;
        StringBuilder builder = new StringBuilder();

        //遍历每一行
        while ((lineContent = reader.readLine()) != null) {
            lineNum++;
            //检查当前行是否应该删除
            boolean isHas = false;
            for (String content : contentList) {
                if (lineContent.contains(content)) {
                    isHas = true;
                    break;
                }
            }
            //删除当前行
            if (isHas) {
                StringKit.formatPrint("删除当前行 [{}] {}", lineNum, lineContent);
                continue;
            }
            //不删除
            builder.append(lineContent);
            builder.append(System.lineSeparator());
        }
        reader.close();

        // 将新的文件内容写入原文件中
        FileWriter writer = new FileWriter(file);
        writer.write(builder.toString());
        writer.close();
    }


    public static ArrayList<Integer> getLinesByContent(File file, String content) throws Exception {
        ArrayList<String> contentList = new ArrayList<>();
        contentList.add(content);
        return getLinesByContent(file, contentList);
    }

    public static ArrayList<Integer> getLinesByContent(File file, ArrayList<String> contentList) throws Exception {
        ArrayList<Integer> lines = new ArrayList<>();
        // 读取文件内容
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String lineContent;
        int lineNum = 0;

        //遍历每一行
        while ((lineContent = reader.readLine()) != null) {
            lineNum++;
            for (String content : contentList) {
                if (lineContent.contains(content)) {
                    StringKit.formatPrint("已找到内容 [{}] [{}] {}行", file.getName(), content, lineNum);
                    lines.add(lineNum);
                }
            }
        }

        reader.close();

        return lines;
    }


    public static void copyTo(String originFilePath, String newFilePath) throws Exception {
        File originFile = new File(originFilePath);
        File newFile = new File(newFilePath);
        copyTo(originFile, newFile, 1);
    }

    public static void copyTo(File originFile, File newFile, int bufferSizeM) throws Exception {
        InputStream inputStream = Files.newInputStream(originFile.toPath());
        OutputStream outputStream = Files.newOutputStream(newFile.toPath());
        byte[] buffer = new byte[1024 * 1024 * bufferSizeM];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }
        outputStream.close();
        inputStream.close();
    }


    public static ArrayList<File> directoryAllFiles(String filePath, ArrayList<File> files) {
        return directoryAllFiles(new File(filePath), files);
    }

    public static ArrayList<File> directoryAllFiles(File file, ArrayList<File> files) {
        // 如果是文件，直接输出文件路径
        if (file.isFile()) {
            files.add(file);
            return files;
        }
        // 如果是目录，递归遍历目录下的文件和子目录
        File[] fileS = file.listFiles();
        if (fileS != null) {
            for (File subFile : fileS) {
                directoryAllFiles(subFile, files);
            }
        }
        return files;
    }


}
